using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WDutils
{
    /// <summary>
    /// Defines the possible error types
    /// </summary>
    public enum ErrorType
    {
        /// <summary>
        /// Defines a Critical error
        /// </summary>
        Critical,
        /// <summary>
        /// Defines a Warning message
        /// </summary>
        Warning,
        /// <summary>
        /// Defines an Information message
        /// </summary>
        Info
    }

    /// <summary>
    /// Logging to file class
    /// </summary>
    public class Log
    {

        private static string logPath = null;

        /// <summary>
        /// Sets the file path to log file
        /// </summary>
        public static string LogFile
        {
           set {
               if (!File.Exists(value))
               {
                   File.Create(value);
               }
               Log.logPath = value;
           }
        }

        /// <summary>
        /// Write a text message to log file while specifing the ErrorType and Exception object
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="et"></param>
        /// <param name="ex"></param>
        public static void WriteToLog(string strMessage, ErrorType et, Exception ex)
        {
            if (Log.logPath != null)
            {
                string strErrorType = "Undefined";
                switch (et)
                {
                    case ErrorType.Critical:
                        strErrorType = "Critical";
                        break;
                    case ErrorType.Info:
                        strErrorType = "Info";
                        break;
                    case ErrorType.Warning:
                        strErrorType = "Warning";
                        break;

                }
                using (StreamWriter s = File.AppendText(Log.logPath))
                {
                    s.WriteLine(DateTime.Now + "\t" + strErrorType + ":" + strMessage);
                    if (ex != null)
                    {
                        s.WriteLine("#Exception# " + ex.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// Write a text message to log file while specifing the ErrorType
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="et"></param>
        public static void WriteToLog(string strMessage, ErrorType et)
        {
            Log.WriteToLog(strMessage, et, null);
        }
       
    }
}
